package model;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import util.Tools;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

public class IndividualDAO {

    @Autowired
    Tools tools;

    public String count(Date end, int interval, String site, String staffId) {
        String loadUrlBase = "http://202.120.117.179:8090/centerload/load?";
        String unloadUrlBase = "http://202.120.117.179:8090/centerunload/load?";
        //String sortUrlBase = "";

        if (!tools.getId2name().has(site)) {
            if (tools.getName2id().has(site)) {
                site = tools.getName2id().getString(site);
            } else {
                return new JSONArray().toString();
            }
        }

        Date start = tools.getStartTime(end, interval);

        HashMap<String, String> loadMap = tools.acquireData(loadUrlBase, start, end, interval, site, null, staffId);
        HashMap<String, String> unloadMap = tools.acquireData(unloadUrlBase, start, end, interval, site, null, staffId);
        //HashMap<String, JSONObject> sortMap = tools.acquireData(sortUrlBase, start, end, interval, site, null,  staffId);

        String[] dates = loadMap.keySet().toArray(new String[]{});
        Arrays.sort(dates);

        JSONArray jsonArray = new JSONArray();

        for (String date : dates) {
            JSONObject t = new JSONObject();
            JSONObject loaddata = new JSONObject(loadMap.get(date));
            JSONObject unloaddata = new JSONObject(unloadMap.get(date));
            //t.put("color", "#000080");
            t.put("loadWeight", loaddata.get("weight"));
            t.put("loadNum", loaddata.get("pieces"));
            t.put("unloadWeight", unloaddata.get("weight"));
            t.put("unloadNum", unloaddata.get("pieces"));
            t.put("sortWeight", 0);
            t.put("sortNum", 0);
            t.put("date", date);

            jsonArray.put(t);
        }

        return jsonArray.toString();
    }

    public int[] weight() {
        return new int[3];
    }

    public int[] countEff() {
        return new int[5];
    }

    public int[] weightEff() {
        return new int[5];
    }


    public String getStaff(Date end, int interval, String site) {
        String staffUrlBase = "http://202.120.117.179:8090/centerload/scanman?";

        if (!tools.getId2name().has(site)) {
            if (tools.getName2id().has(site)) {
                site = tools.getName2id().getString(site);
            } else {
                return new JSONArray().toString();
            }
        }

        Date start = tools.getStartTime(end, interval);

        String startTimestamp = String.valueOf(start.getTime() / 1000);
        String endTimestamp = String.valueOf(end.getTime() / 1000);

        String url = staffUrlBase + "siteId=" + site + "&start=" + startTimestamp + "&end=" + endTimestamp;

        return tools.request(url).toString();
    }
}
